<?php
###############   COPYLEFT GPLv3 LICENSE   ###############
##
## JFX Version 0.2.7
## Website Management Software
## www.jfxcms.com
##
## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
##
## Anthony Gallon
## oi_antz@hotmail.com
##
## Permission is hereby granted to any person having a copy of this software
## to freely use and modify as required so long as the copyright notices
## and branding remain intact.
##
## Full license details available at http://www.jfxcms.com/license
##
###############   COPYLEFT GPLv3 LICENSE   ###############


if(!isset($_SESSION['install']['database'])) redirect('?step=database');
if(!isset($_SESSION['install']['paths'])) redirect('?step=paths');
if(!isset($_SESSION['install']['settings'])) redirect('?step=settings');


if(count($_POST)>0){
    $db = $_SESSION['install']['database'];
    $p = $_SESSION['install']['paths'];
    $s = $_SESSION['install']['settings'];

    require_once($p['baseDir'].'/classes/JFX/Crypt.php');

    mysql_connect($db['host'], $db['user'], $db['pass']);
    mysql_select_db($db['name']);

    $config = <<<CONFIG
<?php
class JFXConfig {
    // database settings
    public \$dbhost = '{$db['host']}';
    public \$dbuser = '{$db['user']}';
    public \$dbpassword = '{$db['pass']}';
    public \$dbname = '{$db['name']}';
    public \$dbprefix = '{$db['prefix']}';

    // path settings
    public \$domain = '';
    public \$baseDir = '{$p['baseDir']}';
    public \$publicDir = '{$p['publicDir']}';
    public \$baseUrl = '';
    public \$jsUrl = '{$p['prefix']}/jfx/js';
    public \$cssUrl = '{$p['prefix']}/jfx/css';
    public \$imageUrl = '{$p['prefix']}/jfx/images';
    public \$adminUrl = '{$p['adminFolder']}';
    public \$adminFolder = '{$p['adminFolder']}';
    public \$adminActionsDir = '{$p['baseDir']}/admin-actions';
    public \$prefix = '{$p['prefix']}';
    public \$classesDir = '{$p['baseDir']}/classes';
    public \$modulesDir = '{$p['baseDir']}/modules';
    public \$tmpDir = '{$p['baseDir']}/tmp';
    public \$themesDir = '{$p['baseDir']}/themes';
    public \$cacheDir = '{$p['baseDir']}/cache';
    public \$moduleCssFolder = '/css-server';
    public \$moduleJsFolder = '/js-server';
    public \$moduleServerFolder = '/module-server';
    public \$moduleImageFolder = '/image-server';
    public \$themeServerFolder = '/theme-server';
    public \$useCache = false;
    public \$useSmartyCache = false;

    // general settings
    public \$adminEmail = '{$s['email']}';
    public \$cryptSalt = '{$s['cryptSalt']}';
    public \$debug = true;
    public \$compressHtml = true;
    public \$tidyHtml = false;
    public \$theme = 'default';

    public function __construct(){
        \$this->domain = \$_SERVER['SERVER_NAME'];
    }

}



/*
 * http://ravenphpscripts.com/postt17133.html
 */
function stripslashes_array(\$striparray)
{
   foreach (\$striparray as \$sec_key => \$secvalue)
   {
      if (is_array(\$secvalue))
      {
         \$striparray[\$sec_key] = stripslashes_array(\$secvalue);
      } else {
         \$striparray[\$sec_key] = stripslashes(\$secvalue);
      }
   }
   return \$striparray;
}

if (get_magic_quotes_gpc())
{
   \$_GET = stripslashes_array(\$_GET);
   \$_POST = stripslashes_array(\$_POST);
   \$_COOKIE = stripslashes_array(\$_COOKIE);
}


global \$CONFIG;
\$CONFIG = new JFXConfig;
\$thisPageUrl = (empty(\$_SERVER['https']))
    ? 'http://'.\$_SERVER['SERVER_NAME'].'/'.\$_SERVER['REQUEST_URI']
    : 'https://'.\$_SERVER['SERVER_NAME'].'/'.\$_SERVER['REQUEST_URI']
    ;

error_reporting(0);






CONFIG;
    $fh = fopen($p['baseDir'].'/jfx-config.php', 'w');

    fwrite($fh, $config);
    fclose($fh);

    if($s['structure']){
        $queries = file_get_contents($dirname.'/install/tables.sql');
        $queries = str_replace('prefix_', $db['prefix'], $queries);
        $queries = explode(';', $queries);
        foreach($queries as $q){
            mysql_query($q);
        }
    }

    if($s['data']){
        $queries = file_get_contents($dirname.'/install/data.sql');
        $queries = str_replace('prefix_', $db['prefix'], $queries);
        $queries = str_replace('__domain__', $p['domain'], $queries);
        $queries = str_replace('__sitename__', $s['sitename'], $queries);
        $queries = explode(';', $queries);
        foreach($queries as $q){
            mysql_query($q);
        }
    }

    $CRYPT = new JFX_Crypt($s['cryptSalt']);
    
    $s['username'] = mysql_real_escape_string($s['username']);
    $s['password'] = mysql_real_escape_string($CRYPT->crypt($s['password']));

    $q = "INSERT INTO {$db['prefix']}admin_users (username, pass) VALUES ('{$s['username']}', '{$s['password']}')";
    mysql_query($q);
    $userid = mysql_insert_id();

    $q = "INSERT INTO {$db['prefix']}admin_users_to_permissions (permission_key, user_id, username) VALUES ('superAdmin', '{$userid}', '{$s['username']}')";
    mysql_query($q);


    // install our modules
    global $CONFIG;
    require($p['baseDir'].'/jfx-config.php');
    require($p['baseDir'].'/classes/JFX/Constants.php');
    require($p['baseDir'].'/classes/JFX/Functions.php');

    global $CONFIG;
    $CONFIG = new JFXConfig;
    $DB = new JFX_Db($CONFIG->dbhost, $CONFIG->dbuser, $CONFIG->dbpassword, $CONFIG->dbname);
    JFX::register('db', $DB);
    JFX::register('config', $CONFIG);
    $LANG = new JFX_Lang;
    JFX::register('lang', $LANG);
    
    $modules = array(
        'core',
        'content',
        'jfxjs',
        'jfxcss',
        'jfxmenu',
        'jfxlinker',
        'croncontrol',
        'iusers',
        'jfxbup'
    );

    foreach($modules as $k=>$v){
        // no need installing if already installed
        if(JFX_Module::isInstalled($v)) continue;

        require_once($CONFIG->modulesDir.'/'.$v.'/'.$v.'.module.php');
        $moduleClassName = 'JFX_Module_'.ucfirst($v);
        $moduleObj = new $moduleClassName;
        if(!is_object($moduleObj)) continue;

        if($moduleObj->install()!==false){
            $dbParams = array(
                'keyname' => $v,
                'version' => $moduleObj->getVersion(),
                'title' => $moduleObj->getTitle(),
                'sorting' => 1+(int) $DB->oneValue($CONFIG->dbprefix.'modules', 'sorting', '1=1 ORDER BY sorting DESC')
            );
            $DB->insert($CONFIG->dbprefix.'modules', $dbParams);
        }
    }

    $themes = array(
        'default'
    );

    foreach($themes as $k=>$v){
        // no need installing if already installed
        if(JFX_Theme::isInstalled($v)) continue;

        require_once($CONFIG->themesDir.'/'.$v.'/'.$v.'.theme.php');
        $themeClassName = 'JFX_Theme_'.ucfirst($v);
        $themeObj = new $themeClassName;
        if(!is_object($themeObj)) continue;

        if($themeObj->install()!==false){
            $dbParams = array(
                'keyname' => $v,
                'version' => $themeObj->getVersion(),
                'title' => $themeObj->getTitle(),
                'sorting' => 1+(int) $DB->oneValue($CONFIG->dbprefix.'themes', 'sorting', '1=1 ORDER BY sorting DESC')
            );
            $DB->insert($CONFIG->dbprefix.'themes', $dbParams);
        }
    }




    redirect($p['adminUrl']);
}



?>

<table>
<tbody>
    <tr>
        <th colspan=2 class="twospan">Database Settings</th>
    </tr>
    <tr>
        <td>Database Host</td><td><?php echo $_SESSION['install']['database']['host'] ?></td>
    </tr>
    <tr>
        <td>Database User</td><td><?php echo $_SESSION['install']['database']['user'] ?></td>
    </tr>
    <tr>
        <td>Database Password</td><td><?php echo $_SESSION['install']['database']['pass'] ?></td>
     </tr>
    <tr>
        <td>Database Name</td><td><?php echo $_SESSION['install']['database']['name'] ?></td>
       </tr>
     <tr>
        <td>Database Prefix</td><td><?php echo $_SESSION['install']['database']['prefix'] ?></td>
       </tr>
    <tr>
        <th colspan=2 class="twospan">Path Settings</th>
       </tr>
    <tr>
        <td>Private Directory</td><td><?php echo $_SESSION['install']['paths']['baseDir'] ?></td>
        </tr>
    <tr>
        <td>Public Directory</td><td><?php echo $_SESSION['install']['paths']['publicDir'] ?></td>
         </tr>

    <tr>
        <td>Admin URL</td><td><?php echo $_SESSION['install']['paths']['adminUrl'] ?></td>
        </tr>
    <tr>
        <td>URL Prefix</td><td><?php echo $_SESSION['install']['paths']['prefix'] ?></td>
         </tr>

    <tr>
        <th colspan=2 class="twospan">General Settings</th>
       </tr>

    <tr>
        <td>Crypt Salt</td><td><?php echo $_SESSION['install']['settings']['cryptSalt'] ?></td>
        </tr>
    <tr>
        <td>Administrator Email</td><td><?php echo $_SESSION['install']['settings']['email'] ?></td>
        </tr>
    <tr>
        <td>Administrator Username</td><td><?php echo $_SESSION['install']['settings']['username'] ?></td>
         </tr>
    <tr>
        <td>Administrator Password</td><td><?php echo $_SESSION['install']['settings']['password'] ?></td>
         </tr>
    <tr>
        <td>Database Structure</td><td><?php if($_SESSION['install']['settings']['structure']) echo 'Will be installed'; else echo 'Will not be installed'; ?></td>
         </tr>
    <tr>
        <td>Sample Data</td><td><?php if($_SESSION['install']['settings']['data']) echo 'Will be installed'; else echo 'Will not be installed'; ?></td>





    </tr>
</tbody>
</table>




<form method="post">
    <p align="center"><input type="submit" name="submit" value="Write to config" /></p>
</form>
